﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class EstTest02
    {
        int[][] orderList;
        int[] fromCount;
        int[] check;
        bool[] finishSearch;
        int recuseCount = 0;
        public EstTest02()
        {
            int testCase = Int32.Parse(TestConsole.ReadLine());
            while (testCase-- > 0)
            {
                string[] input = TestConsole.ReadLine().Split(' ');
                int rightCount = Int32.Parse(input[0]);
                check = new int[rightCount + 1];
                int orderCount = Int32.Parse(input[1]);
                orderList = new int[rightCount + 1][];
                for (int i = 0; i < rightCount + 1; i++)
                {
                    orderList[i] = new int[10];
                }
                int[] destCount = new int[rightCount + 1];
                fromCount = new int[rightCount + 1];
                finishSearch = new bool[rightCount + 1];
                int deep = 0;
                for (int i = 0; i < orderCount; i++)
                {
                    input = TestConsole.ReadLine().Split(' ');
                    int a = Int32.Parse(input[0]);
                    int b = Int32.Parse(input[1]);
                    fromCount[b]++;
                    if (destCount[a] >= orderList[a].Length)
                    {
                        Array.Resize(ref orderList[a], orderList[a].Length + 10);
                    }
                    orderList[a][destCount[a]++] = b;
                }

                for (int i = 0; i < rightCount; i++)
                {
                    int count = destCount[i];
                    for (int j = 0; j < count; j++)
                    {
                        if (orderList[i][j] > 0)
                        {
                            deep++;
                            check[orderList[i][j]] = deep;
                            FindLoop(deep, destCount, i, j);
                        }
                    }
                }
                int output = 0;
                for(int i=1;i<fromCount.Length;i++)
                {
                    if(fromCount[i] == 0)
                    {
                        output++;
                    }
                }
                Console.WriteLine(output);
                TestConsole.ReadLine();
            }
        }
        private void FindLoop(int deep, int[] destCount, int a, int b)
        {
            recuseCount++;
            int nowRight = orderList[a][b];
            if (finishSearch[nowRight])
            { return; }
            for (int i = 0; i < destCount[nowRight]; i++)
            {
                if (orderList[nowRight][i] > 0)
                {

                    if (check[orderList[nowRight][i]] == deep)
                    {
                        Console.WriteLine("루프 찾음 : " + nowRight + " " + orderList[nowRight][i]);
                        int cutLoop = orderList[nowRight][i];
                        for (int j = 0; j < check.Length; j++)
                        {
                            if (check[j] == deep && fromCount[j] > 1)
                            {
                                cutLoop = j;
                                break;
                            }
                        }
                        for (int j = 0; j < destCount[cutLoop]; j++)
                        {
                            if (check[orderList[cutLoop][j]] == deep)
                            {
                                Console.WriteLine("컷팅 : " + cutLoop + " " + orderList[cutLoop][j]);
                                orderList[cutLoop][j] = 0;
                                fromCount[cutLoop]--;
                                
                            }
                        }
                        
                    }
                    else if(check[orderList[nowRight][i]] == 0)
                    {
                        check[orderList[nowRight][i]] = deep;
                        FindLoop(deep, destCount, nowRight, i);
                    }
                    deep++;
                }
            }
            finishSearch[nowRight] = true;
        }

        private long ChangeLong(bool[] check)
        {
            long ret = 0;
            for (double i = 0; i < check.Length; i++)
            {
                ret += (check[(int)i]? 1:0) * (long)Math.Pow((double)2, (double)i);
            }
            return ret;
        }
        private void ChangeBool(ref bool[] check, long val)
        {
            for (int i = 1; i <= check.Length; i++)
            {
                check[i - 1] = (val % 2) == 1;
                val /= 2;
            }
        }
    }

    struct BitArray
    {
        private readonly byte[] setTrueValue;
        private readonly byte[] setFalseValue;
        private long count;
        private byte[] rowData;
        public long Count { get { return count; } }
        public BitArray(int bitCount)
        {
            setTrueValue = new byte[8] { 1,2,4,8,16,32,64,128 };
            setFalseValue = new byte[8] { 254, 253, 251, 247, 239, 223, 191, 127 };
            count = bitCount;
            rowData = new byte[(bitCount - 1) / 8 + 1];
        }
        public bool this[long index]
        {   // Indexer declaration
            get
            {
                return true;
            }
            set
            {
                long byteIndex = (index - 1) / 8 + 1;
                long bitIndex = byteIndex % 8;
                if (value)
                {
                    rowData[byteIndex] |= setTrueValue[bitIndex];
                }
                else
                {
                    rowData[byteIndex] &= setFalseValue[bitIndex];
                }
                
            }
        }
    }
}
